﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Model_SSResourceManager;
using Presenter_SSResourceManager;

namespace WebView_SSResourceManager.UserPages
{
    public partial class ResourceView : Page, IViewResource
    {
        public PresenterResourceView ResourceViewPresenter;
        public static string Resource { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            ResourceViewPresenter = new PresenterResourceView(this);
            ResourceViewPresenter.Refresh(GetResourceId());
            lbl_BookingrResult.Text = string.Empty;
            Resource = GetResourceId().ToString();
        }

        public Guid GetResourceId()
        {
            var query = HttpContext.Current.Request.Url.Query.Where(symbol => symbol != '?');

            try
            {
                return Guid.Parse(new string(query.ToArray()));
            }
            catch (Exception)
            {
                Response.Redirect("~/");
            }

            return new Guid();
        }

        private Guid GetUserID()
        {
            return Guid.Parse(Server.HtmlEncode(Request.Cookies["UID"].Value));
        }

        public void ShowResourceInfo(Resource resource,IEnumerable<Queue> queues)
        {
            lbl_AutoTime.Text = resource.OnlyAutoTime.ToString();
            lbl_Available.Text = (!resource.Locked).ToString();
            lbl_EndTime.Text = resource.EndTime.ToString();
            lbl_ResourceName.Text = resource.Name;
            lbl_StartTime.Text = resource.StartTime.ToString();
            lbl_UseTime.Text = resource.TimeOfUsing.ToString();
            lbl_UserCount.Text = resource.UserCount.ToString();
            lstV_ResourceView.DataSource = queues;
            lstV_ResourceView.DataBind();
        }

        protected void BtnGetInLineClick(object sender, EventArgs e)
        {
            ResourceViewPresenter.BookForNearTime(GetResourceId(),GetUserID());
        }

        protected void BtnBookingClick(object sender, EventArgs e)
        {
            Response.Redirect("~/UserPages/Booking.aspx?" + GetResourceId());
        }


        public void ShowBookingResult(string result)
        {
            lbl_BookingrResult.Text = result;
            lbl_BookingrResult.ForeColor = lbl_BookingrResult.Text[0] == 'S' ? 
                                                    System.Drawing.Color.Red : 
                                                    System.Drawing.Color.Green;
        }

        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.BT_Send.Click += (BT_Send_Click);
            this.Load += (Page_Load);

        }


        public string GetChatPage()
        {
            return ("TheChatScreenWin.aspx");
        }

        private void BT_Send_Click(object sender, System.EventArgs e)
        {
            string sUser = HttpContext.Current.User.Identity.Name;
            if (TB_ToSend.Text.Length > 0)
            {
                Chat.AddMessage(Resource,sUser,TB_ToSend.Text);

                TB_ToSend.Text = "";
            }
        } 

    }
}